package com.environment;


import com.utils.AStarNode;
import com.utils.InvalidException;
import java.util.List;

/**
 * Represents a tile of the game map.
 */
public class Tile extends AStarNode {
    private int row;
    
    private int col;
    
    /**
     * Creates new {@link Tile} object.
     * 
     * @param row row index
     * @param col column index
     */
    public Tile(int row, int col) {
        this.row = row;
        this.col = col;
    }
    
    /**
     * Returns row index.
     * 
     * @return row index
     */
    public int getRow() {
        return row;
    }
    
    /**
     * Returns column index.
     * 
     * @return column index
     */
    public int getCol() {
        return col;
    }
    
    public void addAim(Aim aim) {
        row += aim.getRowDelta();
        col += aim.getColDelta();
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return row * Percept.MAX_MAP_SIZE + col;
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object o) {
        boolean result = false;
        if (o instanceof Tile) {
            Tile tile = (Tile)o;
            result = row == tile.row && col == tile.col;
        }

        return result;
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return "("+row + ":" + col + ")";
    }

    @Override
    public float getCost(AStarNode node) {
        if (!(node instanceof Tile)) try {
            throw new InvalidException("Invalid Arugment to getCost: Must be a Tile.");
        } catch (InvalidException ex) {
//            Logger.getLogger(Tile.class.getName()).log(Level.SEVERE, null, ex);
        }
        Ilk ilk = Percept.getInstance().getIlk((Tile) node);
        
        if (ilk.isPassable()) return 1;

        return 10000;
    }

    @Override
    public float getEstimatedCost(AStarNode node) {
        if (!(node instanceof Tile)) try {
            throw new InvalidException("Invalid Arugment to getEstimatedCost: Must be a Tile.");
        } catch (InvalidException ex) {
//            Logger.getLogger(Tile.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return (float) Percept.getInstance().getDistance2(this, (Tile) node);        
    }

    @Override
    public List getNeighbors() {
        return Percept.getInstance().getNeighbors(this);
    }

}
